home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj007.zip / BLOOM.ZIP / ODBUTTON.C < prev    next >
Text File  |  1992-07-24  |  10KB  |  340 lines

  1. // odbutton.c  ROUTINES TO DRAW OWNER DRAW BUTTONS
  2. // WITH BUTTON FOCUS AND SENDS WM_CTLCOLOR MESSAGE
  3.  
  4. /**************************************************
  5.  
  6. VOID  SetODButton(LPDRAWITEMSTRUCT lpdrawis,
  7.         HBITMAP hbm1,HBITMAP hbm2, WORD aligntext)
  8.  
  9. INPUT:  (LPDRAWITEMSTRUCT)lParam  LongPointer to
  10.           DRAWITEMSTRUCT passed by the owner-draw
  11.           control with the lParam of the
  12.           WM_DRAWITEM message.
  13.          hbm1  Handle to the primary bitmap to be
  14.            drawn on the button face.If NULL no
  15.            bitmaps are expected and drawn.
  16.          hbm2  Handle to the secondary bitmap to be
  17.            drawn when the button is depressed.
  18.            If NULL hbm1 is used for depressed state.
  19.          DrawText wFormat  Used in alining the
  20.            button text. Expects DT_LEFT, DT_CENTER
  21.            or DT_RIGHT
  22.  
  23. OUTPUT:  Sends WM_CTLCOLOR message.
  24.          Responds to text and background colors.
  25.          Background Brush. Fonts.
  26.          One Pen for lower-right unpushed
  27.          corner color.
  28.  
  29. RETURN:  Void
  30.  
  31. **************************************************/
  32.  
  33.  
  34. #include <windows.h>
  35. #include <string.h>
  36. #include "odbutton.h"
  37.  
  38. #define RGB_LTGRAY      RGB(192,192,192)
  39. #define RGB_DARKGRAY    RGB(128,128,128)
  40. #define RGB_MEDIUMGRAY  RGB(190,190,190)
  41. #define RGB_WHITE       RGB(0xFF,0xFF,0xFF)
  42.  
  43. #define MAXLENGTH  20 // Max Length of Button Text
  44.  
  45.  
  46. #define IsButtonSelected(lpd) (lpd->itemAction == ODA_SELECT && lpd->itemState & ODS_SELECTED)
  47.  
  48.  
  49. VOID  SetODButton(LPDRAWITEMSTRUCT lpdrawis,
  50.         HBITMAP hbm1,HBITMAP hbm2, WORD aligntext,
  51.         WORD thickness)
  52.   {
  53.   if (lpdrawis->itemAction & ODA_DRAWENTIRE ||
  54.        lpdrawis->itemAction & ODA_SELECT)
  55.     {
  56.       /* Draw button outline and fill face first */
  57.     DrawODButton((LPDRAWITEMSTRUCT) lpdrawis,
  58.      thickness, OD_BORDER);
  59.  
  60.       /*  Respond to any bitmaps */
  61.     if (hbm1)
  62.       DrawButtonBitmap ((LPDRAWITEMSTRUCT) lpdrawis,
  63.        thickness, OD_BORDER, hbm1, hbm2);
  64.     }
  65.       /* Place button text and focus */
  66.   DrawButtonText ((LPDRAWITEMSTRUCT) lpdrawis,
  67.      thickness, hbm2, OD_BORDER, aligntext);
  68.   }
  69.  
  70.  
  71. VOID DrawODButton(DRAWITEMSTRUCT FAR *lpdrawis,
  72.         WORD wThickness,int border)
  73.   {
  74.   // wThickness  Thickness of the 3D Border.
  75.   //      This is ignored when
  76.   //      the OD_PUSHED style is specified.
  77.   // border == 4  noborder = 0
  78.  
  79.   HWND    hCtlColorBkgd;
  80.   POINT   apt[3];
  81.   WORD    wIndex;
  82.   HBRUSH  hBrush;
  83.   HPEN    hpenLower;
  84.   DWORD   rgbLowerCorner;
  85.   HPEN    hpenUserPassed; // Assumed passed by caller 
  86.                           // parsing WM_CTLCOLOR
  87.   HPEN    hpenUpper;
  88.   DWORD   rgbUpperCorner;
  89.   HDC     hDC;
  90.   WORD    wStyle;
  91.   int     x,y,cx,cy;
  92.   BOOL    bPushed;
  93.  
  94.  
  95.   bPushed = IsButtonSelected(lpdrawis);
  96.   hDC = lpdrawis->hDC;
  97.   x = lpdrawis->rcItem.left;
  98.   y = lpdrawis->rcItem.top;
  99.   cx = lpdrawis->rcItem.right;
  100.   cy = lpdrawis->rcItem.bottom;
  101.  
  102. /* Send WM_CTLCOLOR message to the Dialog parent */
  103.   hCtlColorBkgd = SendMessage(GetParent
  104.       (lpdrawis->hwndItem),WM_CTLCOLOR,
  105.     hDC,MAKELONG(lpdrawis->hwndItem,CTLCOLOR_BTN));
  106.   hBrush = hCtlColorBkgd ? hCtlColorBkgd :
  107.               GetStockObject(WHITE_BRUSH);
  108.   SelectObject (hDC,hBrush);
  109.  
  110.   wStyle = ((!bPushed) ? OD_UP : OD_PUSHED);
  111.   wStyle = wStyle | border;
  112.  
  113.   if(wStyle & OD_BORDER)
  114.     {
  115.     Rectangle(hDC,x,y,cx,cy);
  116.     x ++;
  117.     y ++;
  118.     cx --;
  119.     cy --;
  120.     }
  121.  
  122.     if(wStyle & OD_DOWN || wStyle & OD_PUSHED)
  123.       {
  124.       rgbUpperCorner = RGB_DARKGRAY;
  125.       rgbLowerCorner = RGB_WHITE;
  126.       }
  127.      else
  128.        {
  129.        rgbUpperCorner = RGB_WHITE;
  130.        rgbLowerCorner = RGB_DARKGRAY;
  131.        }
  132.  
  133.     hpenLower = CreatePen(PS_SOLID,1,rgbLowerCorner);
  134.     hpenUpper = CreatePen(PS_SOLID,1,rgbUpperCorner);
  135.     hpenUserPassed   = SelectObject(hDC,hpenLower);
  136.  
  137.     cx --;
  138.     cy --;
  139.  
  140.     for(wIndex = 0;wIndex < wThickness;wIndex ++)
  141.       {
  142.       if(wStyle & OD_PUSHED)
  143.         SelectObject(hDC,hpenUpper);
  144.       else
  145.         SelectObject(hDC,hpenUpper);
  146.       apt[0].x = x + wIndex;
  147.       apt[0].y = cy - wIndex;
  148.       apt[1].x = apt[0].x;
  149.       apt[1].y = y + wIndex;
  150.       apt[2].x = cx + 1 - wIndex;
  151.       apt[2].y = apt[1].y;
  152.       Polyline(hDC,apt,3);
  153.  
  154.       if(wStyle & OD_PUSHED)
  155.         SelectObject(hDC,hpenLower);
  156.       else
  157.         SelectObject(hDC,hpenUserPassed);
  158.  
  159.       apt[0].x ++;
  160.       apt[2].x --;
  161.       apt[1].x = apt[2].x;
  162.       apt[1].y = apt[0].y;
  163.       Polyline(hDC,apt,3);
  164.       }
  165.  
  166.     SelectObject(hDC,hpenUserPassed);
  167.     DeleteObject(hpenLower);
  168.     DeleteObject(hpenUpper);
  169.   }
  170.  
  171.  
  172. BOOL DrawButtonBitmap (DRAWITEMSTRUCT FAR *lpdrawis,
  173.                  WORD wThickness,int border,
  174.                  HBITMAP hbm1, HBITMAP hbm2)
  175. {
  176.   HDC     hDCBits;   /* DC for bitmaps */
  177.   BITMAP  bm;        /* bitmap data struct : width,
  178.                          height, etc. */
  179.   HBITMAP hBM;
  180.   BOOL    ok;        /* ret value from BitBlt()
  181.                           (TRUE if successful) */
  182.   RECT    rect;      /* rect of dialog control
  183.                         for bitmap placement   */
  184.   HDC     hDC;
  185.   WORD    wStyle;
  186.   BOOL    bPushed;
  187.   int     x,y,cx,cy,offset;
  188.  
  189.   bPushed = IsButtonSelected(lpdrawis);
  190.   offset = bPushed;
  191.   hBM = hbm1;
  192.   if (bPushed && hbm2)
  193.     {
  194.     offset = 0;
  195.     hBM = hbm2;
  196.     }
  197.  
  198.   hDC = lpdrawis->hDC;
  199.   x = lpdrawis->rcItem.left;
  200.   y = lpdrawis->rcItem.top;
  201.   cx = lpdrawis->rcItem.right;
  202.   cy = lpdrawis->rcItem.bottom;
  203.  
  204.   if (!hDC || !hBM)  /* if bad hDC or hBM,
  205.                            return FALSE */
  206.     return FALSE;
  207.  
  208.   hDCBits = CreateCompatibleDC(hDC);
  209.   GetObject(hBM, sizeof(BITMAP), (LPSTR)&bm);
  210.   SelectObject(hDCBits, hBM);
  211.  
  212.   x = x + wThickness+1+offset;
  213.   y = y + wThickness+1+offset;
  214.   cx = cx - (wThickness+1)*2;
  215.   cy = cy - (wThickness+1)*2;
  216.  
  217.   ok = StretchBlt(hDC, x, y, cx, cy,
  218.           hDCBits, 0, 0, bm.bmWidth,
  219.           bm.bmHeight,SRCCOPY);
  220.   DeleteDC(hDCBits);
  221.  
  222.   return(ok);
  223. }
  224.  
  225.  
  226. BOOL DrawButtonText (DRAWITEMSTRUCT FAR *lpdrawis,
  227.         WORD wThickness,HBITMAP hbm2,
  228.         int border,WORD aligntext)
  229.   {
  230.  
  231.   BOOL    ok;        /* ret value from BitBlt()
  232.                             (TRUE if successful) */
  233.   RECT    rect;      /* rect of dialog control
  234.                         for bitmap placement   */
  235.   HDC     hDC;
  236.   WORD    CtrlID;
  237.   BOOL    bPushed;
  238.   char    buf[MAXLENGTH];
  239.   int     cx,cy;
  240.   RECT    rectfocus;
  241.   DWORD   textextent;
  242.  
  243.   hDC = lpdrawis->hDC;
  244.   if (aligntext == DT_RIGHT)
  245.     lpdrawis->rcItem.right -= (border + wThickness);
  246.   else if (aligntext == DT_LEFT)
  247.     lpdrawis->rcItem.left += (border + wThickness);
  248.  
  249.   if (lpdrawis->itemAction & ODA_DRAWENTIRE ||
  250.          lpdrawis->itemAction & ODA_SELECT)
  251.     {
  252.     bPushed = IsButtonSelected(lpdrawis);
  253.     if (bPushed && !hbm2)
  254.       {
  255.       lpdrawis->rcItem.top +=2;
  256.       lpdrawis->rcItem.left +=2;
  257.       }
  258.     SendMessage(lpdrawis->hwndItem,WM_GETTEXT,20,
  259.          (LPSTR)buf);
  260.     ok = DrawText(hDC,buf,-1,&lpdrawis->rcItem,
  261.           DT_SINGLELINE | aligntext | DT_VCENTER);
  262.     }
  263.   if (lpdrawis->itemState & ODS_FOCUS)
  264.     {
  265.     SendMessage(lpdrawis->hwndItem,WM_GETTEXT,20,
  266.                   (LPSTR)buf);
  267.     textextent = GetTextExtent(hDC,buf,strlen(buf));
  268.  
  269.     if (textextent == 0)
  270.       {
  271.       rectfocus.left = lpdrawis->rcItem.left+
  272.           ((lpdrawis->rcItem.right-
  273.                lpdrawis->rcItem.left)/4);
  274.       rectfocus.top = lpdrawis->rcItem.top +
  275.           ((lpdrawis->rcItem.bottom-
  276.                  lpdrawis->rcItem.top)/4);
  277.       rectfocus.right = lpdrawis->rcItem.left+
  278.           ((lpdrawis->rcItem.right-
  279.                  lpdrawis->rcItem.left)*3/4);
  280.       rectfocus.bottom = lpdrawis->rcItem.top +
  281.           ((lpdrawis->rcItem.bottom-
  282.                   lpdrawis->rcItem.top)*3/4);
  283.       }
  284.  
  285.     else if (aligntext == DT_RIGHT)
  286.       {
  287.       rectfocus.left = lpdrawis->rcItem.right -
  288.                            LOWORD(textextent);
  289.       rectfocus.top = lpdrawis->rcItem.top+
  290.            ((lpdrawis->rcItem.bottom
  291.          -lpdrawis->rcItem.top)/2)-
  292.          (HIWORD(textextent)/2);
  293.       rectfocus.right = lpdrawis->rcItem.right;
  294.       rectfocus.bottom = lpdrawis->rcItem.top+
  295.            ((lpdrawis->rcItem.bottom
  296.            -lpdrawis->rcItem.top)/2)+
  297.            (HIWORD(textextent)/2);
  298.       }
  299.     else if (aligntext == DT_CENTER)
  300.       {
  301.       rectfocus.left = lpdrawis->rcItem.left+
  302.                  ((lpdrawis->rcItem.right
  303.                  -lpdrawis->rcItem.left)/2)-
  304.                  (LOWORD(textextent)/2);
  305.       rectfocus.top = lpdrawis->rcItem.top+
  306.                  ((lpdrawis->rcItem.bottom
  307.                  -lpdrawis->rcItem.top)/2)-
  308.                  (HIWORD(textextent)/2);
  309.       rectfocus.right = lpdrawis->rcItem.left+
  310.                  ((lpdrawis->rcItem.right
  311.                       -lpdrawis->rcItem.left)/2)+
  312.                       (LOWORD(textextent)/2);
  313.       rectfocus.bottom = lpdrawis->rcItem.top+
  314.                  ((lpdrawis->rcItem.bottom
  315.                  -lpdrawis->rcItem.top)/2)+
  316.                  (HIWORD(textextent)/2);
  317.       }
  318.     if (aligntext == DT_LEFT)
  319.       {
  320.       rectfocus.left = lpdrawis->rcItem.left;
  321.       rectfocus.top = lpdrawis->rcItem.top+
  322.                     ((lpdrawis->rcItem.bottom
  323.                     -lpdrawis->rcItem.top)/2)-
  324.                     (HIWORD(textextent)/2);
  325.       rectfocus.right = lpdrawis->rcItem.left +
  326.                      LOWORD(textextent);
  327.       rectfocus.bottom = lpdrawis->rcItem.top+
  328.                      ((lpdrawis->rcItem.bottom
  329.                         -lpdrawis->rcItem.top)/2)+
  330.                         (HIWORD(textextent)/2);
  331.       }
  332.  
  333.     DrawFocusRect(hDC,&rectfocus);
  334.     }
  335.  
  336.   return(ok);
  337. }
  338.  
  339.  
  340.